package Controrell;

import entity.Person;
import entity.Userinfo;
import lombok.Data;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import service.MainService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/User")
@Data
public class MainControrell {
    @Resource(name = "userService")
    private MainService mainService;

    @RequestMapping("/User")
    public String Main(){
        return "WEB-INF/jsp/UserInfo/UserInfoList";
    }
    @RequestMapping("/GetAll")
    @ResponseBody
    public Map<String,Object> GetAll(int pageNumber, int pageSize, String name){
        name=name==null?"":name;
        Map<String,Object> map=new HashMap<String, Object>();
        Map<String,Object> map1=new HashMap<String, Object>();
        map1.put("name",name);
        map1.put("offset",(pageNumber-1)*pageSize);
        map1.put("limit",pageSize);
        map.put("total",mainService.GetPageCount(name));
        map.put("rows",mainService.GetAll(map1));
        return map;
    }

    @ResponseBody
    @RequestMapping("/GetPersonAllInfo")
    public Map<String,Object> GetPerson(Person person, @RequestParam(value = "pageNumber",defaultValue = "1",required = false) int PageNumber,@RequestParam(value = "pageSize",defaultValue = "5",required = false) int PageSize){
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("total",mainService.GetPersonCount(person));
        map.put("rows",mainService.GetPersonInfo(person.getName(),person.getSex(),PageSize,PageNumber));
        return map;
    }

}
